package com.atguigu.crowd.funding.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atguigu.crowd.funding.entity.Role;
import com.atguigu.crowd.funding.entity.RoleExample;
import com.atguigu.crowd.funding.mapper.RoleMapper;
import com.atguigu.crowd.funding.service.api.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class RoleServiceImpl implements RoleService{

	@Autowired
	private RoleMapper rolemapper;

	@Override
	public void save(Role role) {
		
		rolemapper.insert(role);
	}

	@Override
	public PageInfo<Role> queryRoleBykeyWord(Integer pageNum,Integer pageSize,String keyWord) {
		
		//1、开启pagehaler
		PageHelper.startPage(pageNum, pageSize);
		//2、查询数据
		List<Role> list=rolemapper.queryRoleBykeyWord(keyWord);
		//3、封装分页数据
		PageInfo<Role> pageInfo=new PageInfo<Role>(list);
		
		return pageInfo;
	}

	@Override
	public void batchDelete(List<Integer> roleidList) {
		// TODO Auto-generated method stub
		RoleExample exam=new RoleExample();
		exam.createCriteria().andIdIn(roleidList);
		rolemapper.deleteByExample(exam);
	}

	@Override
	public void updateRole(Role role) {
		// TODO Auto-generated method stub
		rolemapper.updateByPrimaryKey(role);
	}

	@Override
	public List<Role> getAssignRole(Integer adminid) {
		// TODO Auto-generated method stub
		List<Role> list = rolemapper.getAssignRole(adminid);
		return list;
	}

	@Override
	public List<Role> getUnAssignRole(Integer adminid) {
		// TODO Auto-generated method stub
		List<Role> list = rolemapper.getUnAssignRole(adminid);
		return list;
	}

	@Override
	public void updateAssignRole(Integer adminid, List<Integer> listRoleId) {
		// TODO Auto-generated method stub
		//1。先删掉用户-角色中间表中adminid用户所数据
		rolemapper.removeAllRoleByAdmin(adminid);
		//2.重新保存adminid用户的用户-角色中间数据,即重新给它分配角色
		rolemapper.reAssignRole(adminid,listRoleId);
		
	}
	
}
